# -*- coding: UTF-8 -*-

from controller.BaseController import BaseController

class ReportShareStatistic(BaseController):

    __table_style = 'font-size:12px;color:#333333;min-width:500px;text-align:center;border-collapse: collapse;word-break:break-all;'
    __th_style= 'padding:8px;background-color:#dedede;border:1px solid #696969;word-break:break-all;'
    __td_style = 'padding:8px;border:1px solid #696969;word-break:break-all;'


    def execute(self):
        content = []
        last24Hour = int(self.core.loader.module('time').time()) - 86400
        wechatShareInfoStatisticService = self.core.loader.service('WechatShareInfoStatisticService')
        fetchWxxcDataRes = wechatShareInfoStatisticService.sumMiniprogramData(last24Hour)
        fetchH5DataRes = wechatShareInfoStatisticService.sumH5Data(last24Hour)
        
        htmlContent = self.__createWxxcMailContent(fetchWxxcDataRes.data)
        if htmlContent:
            content.append(htmlContent)

        htmlContent = self.__createAccountMailContent(fetchH5DataRes.data)
        if htmlContent:
            content.append(htmlContent)

        
        if content:
            content = '<br>'.join(content)
        else:
            content = '近一天无热门分享数据'

        emailContent = {'title': '微信群消息热门分享', 'content' : content}
        sendmail = self.core.loader.instance('lib.sendmail', 'sendmail', transport_core = self.core)
        sendRes = sendmail(['2237971123@qq.com', 'td_member@beibei.com', 'jing.liu@beibei.com','sang.li@beibei.com','wei.lin@beibei.com','binbin.dong@beibei.com','fei.huang@beibei.com','enen.han@beibei.com','songyi.zhang@beibei.com','xiaorong.shuai@beibei.com'], '微信群消息热门分享', content)
        print sendRes.message

    def __createWxxcMailContent(self, data):
        if not data:
            return ''

        head = '''
            <table style="%s"><thead><tr><th colspan="4" style="%s">小程序最近一天内热门分享</th></tr><tr><th style="width:30%%;%s">小程序名称</th><th style="%s">标题</th><th style="width:20%%;%s">分享次数</th></tr></thead><tbody>
        '''
        head = head % (self.__table_style, self.__th_style, self.__th_style, self.__th_style, self.__th_style)
        tail = '</tbody></table>'
        for item in data:
            head += '<tr>'
            head += '<td style="%s"><a href="%s">%s</a></td>' % (self.__td_style, item[3], item[1].decode('utf-8'))
            head += '<td style="%s">%s</td>' % (self.__td_style, item[0].decode('utf-8'))
            head += '<td style="%s">%d</td>' % (self.__td_style, item[2])
            head += '</tr>'

        return head + tail
    

    def __createAccountMailContent(self, data):
        if not data:
            return ''

        head = '''
            <table style="%s"><thead><tr><th colspan="4" style="%s">公众号最近一天内热门分享</th></tr><tr><th style="width:25%%;%s">热门链接</th><th style="width:25%%;%s">标题</th><th style="%s">完整链接</th><th style="width:15%%;%s">分享次数</th></tr></thead><tbody>
        '''
        head = head % (self.__table_style, self.__th_style, self.__th_style, self.__th_style, self.__th_style, self.__th_style)
        tail = '</tbody></table>'
        for item in data:
            head += '<tr>'
            head += '<td style="%s">%s</td>' % (self.__td_style, item[0].decode('utf-8'))
            head += '<td style="%s">%s</td>' % (self.__td_style, item[1].decode('utf-8'))
            head += '<td style="%s">%s</td>' % (self.__td_style, item[2].decode('utf-8'))
            head += '<td style="%s">%d</td>' % (self.__td_style, item[3])
            head += '</tr>'
        
        return head + tail
